/**
* 包络检波，模拟了硬件半波检波的过程
* rct = 0 时初始化
* rct 是一阶低通滤波器的时间常数
**/
double env_1(double x, double rct)
{
    static double old_y = 0.0;
    if (rct == 0.0)
    {
        old_y = 0.0;
    }
    else
    {
        if (x > old_y)
        {
            old_y = x;
        }
        else
        {
            old_y *= rct / ( rct + 1 );
        }
    }
    return old_y;
}
/**
* 包络检波，模拟了硬件半波检波的过程
* rct = 0 时初始化
* rct 是一阶低通滤波器的时间常数
**/
void env_2(double x[], double y[], int N, double rct)
{
    double xx = 0.0;
    int i;
    y[0] = fabs(x[0]);
    for (i = 1; i < N; i++)
    {
        if ( x[i] > y[i-1])
        {
            y[i] = x[i];
        }
        else
        {
            y[i] = y[i-1] * rct / ( rct + 1 );
        }
    }

}

/**
 * 包络检波，模拟了硬件全波检波的过程
 * rc = 0 时初始化
 **/
double env_3(double x, double rct)
{
    static double old_y = 0.0;
    if(rct == 0.0)
    {
        old_y = 0.0;
    }
    else
    {
        x = fabs(x);
        if(x > old_y)
        {
            old_y = x;
        }
        else
        {
            old_y *= rct / ( rct + 1 );
        }
    }
    return old_y;
}

/**
 * 包络检波，模拟了硬件全波检波的过程
 * rc = 0 时初始化
 **/
void env_4(double x[], double y[], int N, double rct)
{
    double xx = 0.0;
    int i;
    y[0] = fabs(x[0]);
    for(i = 1; i < N; i++)
    {
        xx = fabs(x[i]);
        if( xx > y[i-1])
        {
            y[i] = xx;
        }
        else
        {
            y[i] = y[i-1] * rct / ( rct + 1 );
        }
    }
}